package com.cim.web;

import com.cim.config.GlobalConfig;
import com.cim.pojo.Account;
import com.cim.pojo.Course;
import com.cim.pojo.CourseAccount;
import com.cim.pojo.Video;
import com.cim.service.AccountService;
import com.cim.service.CourseAccountService;
import com.cim.service.CourseService;
import com.cim.service.VideoService;
import com.cim.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags="课时相关")
@RestController
@RequestMapping("/api/lesson")
public class LessonController {

    @Autowired
    VideoService videoService;
    @Autowired
    CourseAccountService courseAccountService;
    @Autowired
    AccountService accountService;
    @Autowired
    CourseService courseService;
    @ApiOperation(value="创建课时",notes="创建课时-与课时对应的是视频或者直播")
    @PostMapping("/create")
    public Object createVideo(@RequestParam @ApiParam("视频标题") String videoName,
                              @RequestParam @ApiParam("是否直播") String live,
                              @RequestParam(required = false) @ApiParam("视频地址(非直播时请填写)") String videoAddress,
                              @RequestParam(required = false) @ApiParam("直播开始时间") String startTime,
                              @RequestParam(required = false) @ApiParam("直播结束时间") String endTime,
                              @RequestParam @ApiParam("课程Id") String courseId,
                              HttpServletRequest request){
        String role= (String) request.getAttribute(GlobalConfig.TOKEN_ROLE);
        Course course=courseService.getCourse(courseId);
        if(role!=null&&role.equals("true")){
            if(course==null){
                return ResponseUtil.fail(404,"找不到课程");
            }
            String currentTime=String.valueOf(System.currentTimeMillis()/1000);
            Video video=new Video(videoName,live,0,videoAddress,startTime,endTime,null,null,null,courseId,currentTime);
            Video res=videoService.addVideo(video);
            course.setReferTime(currentTime);
            courseService.updateCourse(course); //创建课时更新 课程更新时间为现在
            return ResponseUtil.ok(res);
        }else{
            return ResponseUtil.unauthz();
        }
    }

    @ApiOperation("修改课时")
    @PostMapping("/modifi")
    public Object modifi(@RequestParam @ApiParam("课时ID") String videoId,
                         @RequestParam(required = false) @ApiParam("视频标题") String videoName,
                         @RequestParam(required = false) @ApiParam("是否直播") String live,
                         @RequestParam(required = false) @ApiParam("视频地址(非直播时请填写)") String videoAddress,
                         @RequestParam(required = false) @ApiParam("直播开始时间") String startTime,
                         @RequestParam(required = false) @ApiParam("直播结束时间") String endTime,
                         HttpServletRequest request){
        String user=(String)request.getAttribute(GlobalConfig.TOKEN_USER);
        String role=(String)request.getAttribute(GlobalConfig.TOKEN_ROLE);
        Video video=videoService.getVideo(videoId);
        if(video==null){
            return ResponseUtil.fail(404,"找不到课时");
        }
        //权限认证 判断是否教师,判断课程ID和用户ID是否有关系
        Account account=accountService.getAccountByUserName(user);
        if(role!=null&&role.equals("true")&&account!=null){    //判断用户是否存在 并且角色为教师
            CourseAccount ca=courseAccountService.findByCA(account.getUserId(),video.getCourseId());
            if(ca!=null){   //判断用户和课程关系是否成立
                //开始修改
                video.setVideoName(videoName);
                video.setLive(live);
                video.setVideoAddress(videoAddress);
                video.setStartTime(startTime);
                video.setEndTime(endTime);
                videoService.updateVideo(video);
                return ResponseUtil.ok(video);
            }
        }
        return ResponseUtil.unauthz();
    }

    @ApiOperation("删除课时")
    @PostMapping("/delete")
    public Object deleteCourse(@RequestParam @ApiParam("课时(视频)Id") String videoId,
                               HttpServletRequest request){
        String user=(String)request.getAttribute(GlobalConfig.TOKEN_USER);
        String role=(String)request.getAttribute(GlobalConfig.TOKEN_ROLE);
        Video video=videoService.getVideo(videoId);
        if(video==null){
            return ResponseUtil.fail(404,"找不到课时");
        }
        //权限认证 判断是否教师,判断课程ID和用户ID是否有关系
        Account account=accountService.getAccountByUserName(user);
        if(role!=null&&role.equals("true")&&account!=null){    //判断用户是否存在 并且角色为教师
            CourseAccount ca=courseAccountService.findByCA(account.getUserId(),video.getCourseId());
            if(ca!=null){   //判断用户和课程关系是否成立
                //删除
                Video v=videoService.getVideo(videoId);
                if(v!=null){
                    videoService.deleteVideo(videoId);
                    return ResponseUtil.ok();
                }
                return ResponseUtil.fail(404,"找不到课程");
            }
        }
        return ResponseUtil.unauthz();
    }

    @ApiOperation("获取课时(视频)信息")
    @PostMapping("/get")
    public Object getVideo(@RequestParam(required = false) @ApiParam("课时（视频）Id") String videoId,
                           @RequestParam(required=false) @ApiParam("课程Id") String courseId){
        Video video=new Video();
        if(videoId==null||videoId.equals("")){
            if(courseId==null){
                return ResponseUtil.ok();
            }
            video.setCourseId(courseId);
            List<Video> list=videoService.findVideos(video);
            if(list.size()>0){
                video=list.get(0);
            }
        }else{
            video=videoService.getVideo(videoId);
            if(video==null){
                return ResponseUtil.ok();
            }
        }
        return ResponseUtil.ok(video);
    }
}
